#include "Enemy.h"
#include "Configuration.h"
#include "Controler.h"

void Enemy::Display()
{
	if(!IsAlive)
		return;

	glPushMatrix();	
	glTranslatef(Position.x,Position.y,Position.z);
	
	glColor3f(ObjectColor.r,ObjectColor.g,ObjectColor.b);
	glBegin(GL_TRIANGLES);
	for (int i = 0 ; i < polygon_qty ; i ++)
	{
		glVertex3f(pVertices[pPolygons[i].a].x,pVertices[pPolygons[i].a].y,pVertices[pPolygons[i].a].z);
		glVertex3f(pVertices[pPolygons[i].b].x,pVertices[pPolygons[i].b].y,pVertices[pPolygons[i].b].z);
		glVertex3f(pVertices[pPolygons[i].c].x,pVertices[pPolygons[i].c].y,pVertices[pPolygons[i].c].z);
	}
	glEnd();
	glPopMatrix();
}

void Enemy::Initialise(char* initFile)
{

}

void Enemy::SetPosition(Point newPosition)
{
	Position = newPosition;
}

void Enemy::Initialise(char* initFile,Color& color,Point& position)
{
	Position.x = position.x;
	Position.y = position.y;
	Position.z = position.z;
	direction = Point(-1,0,0);
	speed = 0.4;
	p_3dsFileName = initFile;
	LoadObjectMesh();
	hitBox.LoadAndInitialise("models/enemy1HB.3ds",position);
	ObjectColor = color;
	attackPower = 2;
}

bool Enemy::Move()
{	
	if(health == 0)
	{
		IsAlive = false;
		return false;
	}
	Position.x += direction.x*speed*Config::GameSpeed;
	Position.y += direction.y*speed*Config::GameSpeed;
	Position.z += direction.z*speed*Config::GameSpeed;

	hitBox.moveToPosition(Position);

	if (Position.x > (Config::TopRightCorner.x + 100)  ||
		Position.x < -Config::TopRightCorner.x ||
		Position.y > Config::TopRightCorner.y  ||
		Position.y < -Config::TopRightCorner.y)
	{
		IsAlive = false;
		health = 0;
		Controler::globalKills++;
		return false;		
	}
	return true;
}